﻿using Chloe;
using Chloe.MySql;
using System;
using System.Collections.Generic;

namespace Ywdsoft.Chloe
{
    public class DbContextFactory : IDbContextFactory
    {
        private readonly string _dbConfig;

        private readonly Dictionary<string, MySqlContext> _contexts = new Dictionary<string, MySqlContext>();

        public DbContextFactory(string dbConfig)
        {
            _dbConfig = dbConfig;
        }

        public DbContext CreateDbContext(Type tableType)
        {
            var context = new MySqlContext(new MySqlConnectionFactory(_dbConfig));
            _contexts[_dbConfig] = context;
            return context;
        }

        public void Dispose()
        {
            foreach (var context in _contexts.Values)
            {
                context.Dispose();
            }
        }
    }
}